// pages/waybillDetail/waybillDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logistics_id: 0,                    //揽件单ID
    waybillType: "",                    // 揽件类型（未揽件，已揽件）用于区分展示
    post_communicate_name: "",                     // 寄件人姓名
    post_communicate_mobile: "",                    // 寄件人电话
    receive_communicate_name: "",                  // 收件人姓名
    receive_communicate_mobile: "",                 // 收件人电话
    arrive_region_name: "",               // 收件站点
    goods_type: "",                      // 货物类型
    weight: "",                    // 货物重量
    num: "",                     // 货物数量
    fileList:[],                       // 照片
    expected_money: "",                           // 运费
    pay: "",                             // 付款方式  
    goods_name: "",                 // 寄件物名称     
    status: 0,       //托运单状态
    isShowGoods: false,    //展示货物信息pop
    goodsTypeLists: [],
    goodsType: '',
    goodsWeight: '',
    goodsName: '',
    post_communicate_address: '',
    receive_communicate_address: '',
    logistics_no: "",
    company_type: 0
  },

  /**
   * 获取揽件详情信息
   */
  getPackageDetail: function () {
    let data = {}
    wx.$api.getPackageDetail(data).then(res=>{

    })
  },
  /**
   * 修改运费
   * @param {*} e 
   */
  onMoneyChange(e){
    this.setData({
      expected_money: e.detail
    })
  },
  onChange(e){
    this.setData({
      goods_name: e.detail
    })
  }, 

  showGoodsPopup(){
    this.setData({
      isShowGoods: true,
      goodsName: this.data.goods_type,
      goodsWeight: this.data.weight
    })
  },

  closeGoodsPopup(){
    this.setData({
      isShowGoods: false,
      goodsName: '',
      goodsWeight: ''
    })
  },

    /**
   * 选择货物类型
   */
  changeGoodsType: function (e) {
    let index = e.currentTarget.dataset.index;
    let goodsName = "";
    if(index == undefined){
      goodsName = e.detail;
    }else{
      goodsName = this.data.goodsTypeLists[index].name;
    }
    this.setData({
      goodsTypeLists:  this.data.goodsTypeLists.map((item, i)=>{
        if(index === i){
          item.isActive = true;
        }else{
          item.isActive = false;
        }
        return item;
      }),
      goodsName: goodsName
    })
  },
  getScancode: function () {
    var _this = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      scanType: ['qrCode', 'barCode'],
      success: (res) => {
        var result = res.result;
        _this.setData({
          logistics_no: result,
        })
      }
    })
  },
  changeLogisticsNo(e) {
    this.setData({
      logistics_no: e.detail
    })
  },
    /**
   * 选择货物重量
   * @param {*} e 
   */
  changeGoodsWeight: function (e) {
    this.setData({
      goodsWeight: e.detail
    })
  },

    /**
   * 提交货物信息
   */
  submitGoodsInfo: function () {
    this.setData({
      isShowGoods: false,
      weight: this.data.goodsWeight,
      goods_type: this.data.goodsName,
      goodsName: '',
      goodsWeight: ''
    })
  },

  beforeRead(event) {
    const { file, callback } = event.detail;
    callback(file.type === 'image');
  },

  afterRead(event) {
    let that = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    console.log('file', file);
    wx.uploadFile({
      url: wx.$config.serverAddress + 'upimg', // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: { 'file':file.path},
      success(res) {
        let get_data = JSON.parse(res.data);
        if (get_data.code != 200){
          wx.$Toast(get_data.msg);
          return;
        }
        // 上传完成需要更新 fileList
        console.log('suceess', get_data);
        const fileList = that.data.fileList;
        fileList.push({ ...file, url: get_data.data.url });
        console.log('fileList', fileList);
        that.setData({ fileList });
      }
    });
  },

  /**
   * 提交付款方式
   * @param {*} e 
   */
  submitPayType: function (e) {
    let fileList = this.data.fileList.map(item=>{
      return item.url
    })
    let data = {
      logistics_id: this.data.logistics_id,
      pay_type: e.currentTarget.dataset.type,
      pictures_urls: fileList.join(','),
      goods_name: this.data.goods_name,
      goods_type: this.data.goods_type,
      weight: this.data.weight,
      expected_money:this.data.expected_money
    } 
    wx.$api.postLogistics(data).then(res=>{
      if(res.data.code != 200 && res.data.code != 201)    return  wx.$Toast(res.data.msg);
      if(res.data.code == 201){
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          confirmColor: "#f00",
          cancelColor: "#56D53B",
          success: res => {
            wx.navigateTo({
              url: '/pages/waybill/waybill',
            })
          }
        })
      }else{
        wx.$Toast(res.data.msg);
        wx.navigateTo({
          url: '/pages/waybill/waybill',
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('come options', options);
    this.setData({
      waybillType: options.type,
      logistics_id: options.logistics_id
    })

    this.detailLogistics();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取揽件详情
   */
  detailLogistics: function(){
    wx.$api.detailLogistics({ logistics_id: this.data.logistics_id}).then(res => {
      if(res.data.code != 200)    return  wx.$Toast(res.data.msg);
      let get_data = res.data.data;
      let isCustomGoodsType = false;
      let goodsTypeLists = get_data.good_type.map(item=>{
        if(get_data.goods_type == item.name){
          item.isActive = true;
          isCustomGoodsType = false;
        }else{
          isCustomGoodsType = true;
        }
        return item;
      })
      this.setData({
        post_communicate_name: get_data.post_communicate_name,                     // 寄件人姓名
        post_communicate_mobile: get_data.post_communicate_mobile,                    // 寄件人电话
        receive_communicate_name: get_data.receive_communicate_name,                  // 收件人姓名
        receive_communicate_mobile: get_data.receive_communicate_mobile,                 // 收件人电话
        arrive_region_name: get_data.arrive_region_name,               // 收件站点
        goods_type: get_data.goods_type,                      // 货物类型
        goods_name: get_data.goods_name,                      // 货物类型
        weight: get_data.weight,                    // 货物重量
        photoList: get_data.pictures,                       // 照片
        expected_money: get_data.expected_money,                           // 运费
        pay: get_data.pay_type_name,                             // 付款方式  
        status: get_data.status,
        fileList: get_data.fileList,
        goodsTypeLists: goodsTypeLists,
        goodsType: isCustomGoodsType ? get_data.goods_type :'',
        post_communicate_address: get_data.post_communicate_address,
        receive_communicate_address: get_data.receive_communicate_address,
        logistics_no: get_data.logistics_no,
        company_type: get_data.company_type
      });
    });

  }

})